//
//  MNDAboutViewController.m
//  minidai
//
//  Created by Steven.Yung on 2017/5/26.
//  Copyright © 2017年 迷你金服. All rights reserved.
//

#import "MNDAboutViewController.h"
#import "UIImage+JKColor.h"
#import "UIImage+Compress.h"
#import <StoreKit/StoreKit.h>


@interface MNDAboutViewController ()

@property (weak, nonatomic) IBOutlet UILabel *versionLabel;
@property (weak, nonatomic) IBOutlet UILabel *wechatLabel;
@property (weak, nonatomic) IBOutlet UILabel *emailLabel;
@property (weak, nonatomic) IBOutlet UILabel *webpageLabel;
@property (weak, nonatomic) IBOutlet UILabel *servicePhoneLabel;

@property (strong, nonatomic) UIView *headerView;
@property (strong, nonatomic) UIView *footerView;

@property (nonatomic) CGRect headerFrame;

@end

@implementation MNDAboutViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //适配iOS11
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    // app版本
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    self.versionLabel.text = [NSString stringWithFormat:@"V%@", app_Version];
    
    self.tableView.tableHeaderView = self.headerView;
    self.tableView.tableFooterView = self.footerView;
 
    [self initialLeftBarButtons];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    UIImage *image = [UIImage new];
    [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = image;
    
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage jk_imageWithColor:[UIColor whiteColor]] forBarMetrics:UIBarMetricsDefault];
    
    UIImage *image = [UIImage jk_imageWithColor:[UIColor colorWithRed:239/255.0 green:239/255.0 blue:244/255.0 alpha:0.3]];
    self.navigationController.navigationBar.shadowImage = image;
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor blackColor]}];
}

- (void)initialLeftBarButtons {
    UIImage *backImage = [UIImage imageNamed:@"navi_back_btn_black"];
    
    //创建返回按钮
    UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithImage:backImage
                                                                          style:UIBarButtonItemStylePlain
                                                                         target:self
                                                                         action:@selector(backAction:)];
    //设置返回按钮的颜色
    [backBarButtonItem setTintColor:[UIColor whiteColor]];
    
    //创建UIBarButtonSystemItemFixedSpace
    UIBarButtonItem * spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                                                                target:nil action:nil];
    //将宽度设为负值
    spaceItem.width = kScreenWidth < 414? -10 : -15;
    //将两个BarButtonItem都返回给NavigationItem
    if (@available(iOS 11.0, *)) {
        self.navigationItem.leftBarButtonItem = backBarButtonItem;
    } else {
        self.navigationItem.leftBarButtonItems = @[spaceItem, backBarButtonItem];
    }
}

- (void)backAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (UIView *)headerView {
    if (!_headerView) {
        
        UIImage *headerImage = [UIImage imageNamed:@"about_header"];
        UIImageView *imageView = [[UIImageView alloc] init];
        
        if (kDevice_iPad) {
            self.headerFrame = CGRectMake(0, 0, kScreenWidth, 400);
            [imageView setImage:[headerImage compressionRatioImageWithSize:CGSizeMake(CGRectGetWidth(self.headerFrame), CGRectGetHeight(self.headerFrame))]];
        } else {
            self.headerFrame = CGRectMake(0, 0, kScreenWidth, headerImage.size.height + 10);
            [imageView setImage:headerImage];
        }
        
        _headerView = [[UIView alloc] initWithFrame:self.headerFrame];
        _headerView.backgroundColor = kColorBackground;
        [_headerView addSubview:imageView];
        WeakSelf(self);
        
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakself.headerView);
            make.left.equalTo(weakself.headerView);
            make.right.equalTo(weakself.headerView);
            make.bottom.equalTo(weakself.headerView).offset(-10.0);
        }];
    }
    return _headerView;
}

- (UIView *)footerView {
    if (!_footerView) {
        
        _footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - self.headerFrame.size.height - 10 - 44 *5)];
        
        UILabel *serviceTimeLabel = [[UILabel alloc] init];
        serviceTimeLabel.text = @"客服工作时间: 09:00~18:00";
        serviceTimeLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:10.0];
        serviceTimeLabel.textColor = kColorGray;
        [_footerView addSubview:serviceTimeLabel];
        
        UILabel *copyLabel = [[UILabel alloc] init];
        copyLabel.text = @"Copyright©2013-2018 Miniwangdai.com";
        copyLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:10.0];
        copyLabel.textColor = kColorGray;
        [_footerView addSubview:copyLabel];
        
        WeakSelf(self);
        
        [copyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(weakself.footerView).offset(-20.0);
            make.centerX.equalTo(weakself.footerView);
        }];
        
        [serviceTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(copyLabel.mas_top).offset(-10.0);
            make.centerX.equalTo(weakself.footerView);
        }];
    }
    
    return _footerView;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.row == 4) {
        NSString *callPhone = [NSString stringWithFormat:@"telprompt://%@", kServicePhoneNumber];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:callPhone]];
    }
    if (indexPath.row == 5) {
        //AppStore评分
        if ([SKStoreReviewController respondsToSelector:@selector(requestReview)]){
            [SKStoreReviewController requestReview];
        } else {
            NSString  * nsStringToOpen = [NSString  stringWithFormat: @"itms-apps://itunes.apple.com/app/id%@?action=write-review",@"1072685010"];//替换为对应的APPID
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:nsStringToOpen]];
        }
    }
}

/*
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#@"reuseIdentifier"#> forIndexPath:indexPath];
    
    // Configure the cell...
    
    return cell;
}
*/

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
